﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Allocatin_Alloction : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);


            initddlWare();

            if (Request["action"] == "allo")
            {
                lblDh.Text = initBarCode();
                initData();
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
            if (Request["action"] == "flag")
            {
                ZSql z = new ZSql();
                z.Open("select * from CS_alloction where alloCode='" + Request["id"] + "'");
                if (z.m_table.Rows[0]["AuditState"].ToString() == "已通过")
                {
                    btnAdd.Enabled = false;
                }
                z.Close();
                ddljsWare.Enabled = false;
                initFlagData();
            }
            
        }
    }
    protected void linkbtnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("SalesManage.aspx?moduid=" + Request["moduid"]);
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ZSql sales = new ZSql();
        //ZSql update = new ZSql();
        ZSql zs = new ZSql();
        ZSql update = new ZSql();
        ZSql add = new ZSql();
        ZSql sel = new ZSql();
        ZSql zup = new ZSql();

        if (string.IsNullOrEmpty(txtNum.Value))
        {
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('请输入购买数量');", true);
            return;
        }

     
        if (Request["action"] == "allo")
        {
          
            sales.Open("select TotalNum from CS_Stock where Comodid='" + Request["id"]+"'");
            if (Convert.ToInt32(txtNum.Value) <= Convert.ToInt32(sales.m_table.Rows[0]["TotalNum"].ToString()))
            {
                string sql = "insert into CS_alloction(alloCode,alloWare,receWare,comodid,brand,comodName,modul,Unit,comodBarcode,Prices,number,Supplier,alloDate,alloMan,content,AuditState)values('" + initBarCode() + "','" + HidWareid.Value + "','" + ddljsWare.SelectedValue + "','" + lblCode.Text + "','" + lblBrand.Text + "','" + lblComoName.Text + "','" + lblSpeci.Text + "','" + lblUnits.Text + "','" + lblBarcode.Text + "'," + lblPrice.Text + "," + txtNum.Value + ",'" + lblSupper.Text + "','" + txtdbdate.Value + "','" + lblName.Text + "','" + Common.FormatParameter(txtContent.Text) + "','未通过')";
                sales.Open(sql);

                //update.Open("update CS_Stock set TotalNum=TotalNum-" + txtNum.Value + " where Comodid=" + Request["id"]);
                sales.Close();
            }
            else
            {
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('输入数量已超出库存数');", true);

            }
            Response.Redirect("ComodAllo.aspx?moduid=" + Request["moduid"]);
        }

        if (Request["action"] == "flag")
        {
            
            if (drbtnflag.Checked)
            {

                zs.Open("select * from CS_alloction where alloCode='" + Request["id"] + "'");
                if (zs.m_table.Rows[0]["AuditState"].ToString()=="已通过")
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('该调拨单已审核');", true);
                    return;
                }
                if (zs.m_table.Rows.Count > 0)
                {
                    update.Open("select * from CS_Stock where WarehouseId='" + zs.m_table.Rows[0]["receWare"].ToString() + "' and Comodid='" + zs.m_table.Rows[0]["Comodid"].ToString() + "'");
                    if (update.m_table.Rows.Count > 0)
                    {
                        update.Open("update CS_Stock set TotalNum=TotalNum+" + zs.m_table.Rows[0]["number"].ToString() + " where WarehouseId='" + zs.m_table.Rows[0]["receWare"].ToString() + "' and Comodid='" + zs.m_table.Rows[0]["Comodid"].ToString() + "'");


                    }
                    else
                    {
                        sel.Open("select * from CS_Stock where Comodid='" + zs.m_table.Rows[0]["comodid"].ToString() + "' and WarehouseId='" + zs.m_table.Rows[0]["alloWare"].ToString() + "'");
                        add.Open("insert into CS_Stock(Producer,Content,WarehouseId,ComoditiesType,BrandId,ComoditiesName,ModelId,UnitId,Comodid,ComodBarcode,Prices,SalesPrices,TotalNum,storageTime,SuppliId,AuditUserName,AuditDate)values('" + sel.m_table.Rows[0]["Producer"].ToString() + "','" + sel.m_table.Rows[0]["Content"].ToString() + "','" + zs.m_table.Rows[0]["receWare"].ToString() + "','" + sel.m_table.Rows[0]["ComoditiesType"].ToString() + "','" + sel.m_table.Rows[0]["BrandId"].ToString() + "','" + sel.m_table.Rows[0]["ComoditiesName"].ToString() + "','" + sel.m_table.Rows[0]["ModelId"].ToString() + "','" + sel.m_table.Rows[0]["UnitId"].ToString() + "','" + sel.m_table.Rows[0]["Comodid"].ToString() + "','" + sel.m_table.Rows[0]["ComodBarcode"].ToString() + "'," + sel.m_table.Rows[0]["Prices"].ToString() + "," + sel.m_table.Rows[0]["SalesPrices"].ToString() + "," + zs.m_table.Rows[0]["number"].ToString() + ",'" + DateTime.Now.ToShortDateString() + "','" + sel.m_table.Rows[0]["SuppliId"].ToString() + "','" + zs.m_table.Rows[0]["alloMan"].ToString() + "','" + DateTime.Now.ToShortDateString() + "')");
                    }
                    string sql = "update CS_Stock set TotalNum=TotalNum-" + zs.m_table.Rows[0]["number"].ToString() + " where WarehouseId='" + zs.m_table.Rows[0]["alloWare"].ToString() + "' and Comodid='" + zs.m_table.Rows[0]["Comodid"].ToString() + "'";
                    zup.Open(sql);
                }

                zs.Open("update CS_alloction set AuditState='已通过',AuditUserName='" + lblName.Text + "',AuditDate='" + DateTime.Now.ToShortDateString() + "' where alloCode='" + Request["id"] + "'");
            }
            zs.Close();
            update.Close();
            add.Close();
            sel.Close();
            zup.Close();
            Response.Redirect("AlloFlag.aspx?moduid=" + Request["moduid"]);
        }



    }

    private void initData()
    {
        ZSql zs = new ZSql();
        zs.Open("select a.content,a.WarehouseId,Prices,producer,salesprices ,stockid,WarehouseName,e.Brand,f.ComoditiesName,i.Specifications,j.Units,a.comodid,ComodBarcode,SalesPrices,totalNum from CS_Stock a join Brand e on a.brandid=e.BrandId join ComoditiesName f on a.ComoditiesName=f.ComoditiesNameId join Specifications i on a.modelid=i.SpecificationsId join Units j on a.Unitid=j.UnitsId join CS_Warehouse h on a.warehouseid=h.warehouseid where a.comodid='" + Request["id"]+"'");

        lblWarehouse.Text = zs.m_table.Rows[0]["WarehouseName"].ToString();
        HidWareid.Value = zs.m_table.Rows[0]["WarehouseId"].ToString();
        txtdbdate.Value = DateTime.Now.ToShortDateString();
        lblCode.Text = zs.m_table.Rows[0]["comodid"].ToString();
        lblBarcode.Text = zs.m_table.Rows[0]["ComodBarcode"].ToString();
        lblBrand.Text = zs.m_table.Rows[0]["Brand"].ToString();
        lblComoName.Text = zs.m_table.Rows[0]["ComoditiesName"].ToString();
        lblSpeci.Text = zs.m_table.Rows[0]["Specifications"].ToString();
        lblUnits.Text = zs.m_table.Rows[0]["Units"].ToString();
        lblSupper.Text = zs.m_table.Rows[0]["producer"].ToString();
        lblPrice.Text = zs.m_table.Rows[0]["Prices"].ToString();



        lblName.Text = lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
    }


    private string initBarCode()
    {
        ZSql zs = new ZSql();
        string id = "XS" + DateTime.Now.ToString("yyyyMMdd");
        zs.Open("select max(alloctionId) as a from CS_alloction");
        if (string.IsNullOrEmpty(zs.m_table.Rows[0]["a"].ToString()))
        {
            id += 1;
        }
        else
        {
            id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        }

        return id;

    }
    private void initddlWare()
    {
        ZSql zs = new ZSql();
        zs.Open("select warehouseid,warehousename from CS_Warehouse");
        ddljsWare.DataSource = zs.m_table;
        ddljsWare.DataTextField = "warehousename";
        ddljsWare.DataValueField = "warehouseid";
        ddljsWare.DataBind();
        ddljsWare.Items.Insert(0, new ListItem("—请选择仓库—", "0"));
        zs.Close();

    }

    private void initFlagData()
    {
        ZSql zs = new ZSql();
        zs.Open("select *,allowarehouse=(select warehousename from CS_Warehouse where warehouseid=a.alloware),recewarehouse=(select warehousename from CS_Warehouse where warehouseid=a.receware ) from CS_alloction a where allocode='" + Request["id"] + "'");

        lblWarehouse.Text = zs.m_table.Rows[0]["allowarehouse"].ToString();
        HidWareid.Value = zs.m_table.Rows[0]["alloWare"].ToString();
        txtdbdate.Value = DateTime.Now.ToShortDateString();
        lblCode.Text = zs.m_table.Rows[0]["comodid"].ToString();
        lblBarcode.Text = zs.m_table.Rows[0]["ComodBarcode"].ToString();
        lblBrand.Text = zs.m_table.Rows[0]["Brand"].ToString();
        lblComoName.Text = zs.m_table.Rows[0]["comodName"].ToString();
        lblSpeci.Text = zs.m_table.Rows[0]["modul"].ToString();
        lblUnits.Text = zs.m_table.Rows[0]["Unit"].ToString();
        lblSupper.Text = zs.m_table.Rows[0]["Supplier"].ToString();
        lblPrice.Text = zs.m_table.Rows[0]["Prices"].ToString();
        lblDh.Text = zs.m_table.Rows[0]["alloCode"].ToString();
        txtNum.Value = zs.m_table.Rows[0]["number"].ToString();
        lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
        ddljsWare.SelectedValue = zs.m_table.Rows[0]["receWare"].ToString();
    }
}
